# -*- coding: utf-8 -*- 

import datetime
import logging
import urllib2

from google.appengine.ext import db
from BeautifulSoup import BeautifulSoup

from model import *

def update_site_sold_total():
    # create group site
    group_site = get_site()

    # init group_info
    group_infos = list()

    # get groups
    today_groups = group_site.today_infos
    
    for group_info in today_groups:
        update_group_sold_total(group_info)
        

def update_group_sold_total(group_info):
    page = urllib2.urlopen(group_info.link)
    soup = BeautifulSoup(page)
    
    sold_total = soup.findAll('span', attrs={'id':'deal-sold-num'})[0].getText()

    group_info.sold_total = int(sold_total)
    group_info.put()

    logging.info("groupinfo updated succeed : %s"%group_info.link)
        

def get_site():
    # create group site
    group_site = GroupSite.get_by_key_name("SITE_sina")
    if group_site is None:
        group_site = GroupSite(key_name="SITE_sina")
        group_site.code = "sina"
        group_site.name = "新浪团".decode('utf8')
        group_site.link = db.Link("http://life.sina.com.cn/tuan/beijing")
        group_site.pop = 70
        group_site.put()
    
    return group_site

def get_data():
    # create group site
    group_site = get_site()

    # init group_info
    group_infos = list()

    # get groups
    today_groups = group_site.today_infos

    if today_groups.count() == 0:
        
        # get site content
        page = urllib2.urlopen(group_site.link)
        
        # soup
        soup = BeautifulSoup(page)
        
        main_div = soup.findAll('div', attrs={'class':'today_buy'})[0]

        title = main_div.findAll('h1')[0].contents[1]
        title = title.replace('"', '')

        img_div = main_div.findAll('div', attrs={'class':'product_img'})[0]
        link = "http://life.sina.com.cn%s"%img_div.findAll('a')[0]['href'].replace('/buy','')
        picture = img_div.findAll('img')[0]['src']

        bug_div = main_div.findAll('div', attrs={'class':'buy_info'})[0]
        current_price = bug_div.findAll('div', attrs={'class':'buy'})[0].findAll('a')[0].contents[0][1:]

        li = bug_div.findAll('li')
        original_price = li[3].getText()[1:]
        discount = li[4].getText()[:-1]

        sold_total = main_div.findAll('strong', attrs={'id':'deal-sold-num'})[0].getText()

        # create group_info
        group_info = GroupInfo()
        group_info.site = group_site
        group_info.title = title
        group_info.current_price = float(current_price)
        group_info.original_price = float(original_price)
        group_info.discount = float(discount)
        group_info.picture = db.Link(picture)
        group_info.link = db.Link(link)
        group_info.sold_total = int(sold_total)
        group_info.create()
        
        group_infos.append(group_info)

        # side items
        items = soup.findAll('div', attrs={'class':'buying'})[0].findAll('ul')[0].findAll('li')
        for item in items:    
            title_a = item.findAll('a')[0]
            title = title_a['title']
            link = "http://life.sina.com.cn%s"%title_a['href']

            picture = item.findAll('img')[0]['src']

            strong = item.findAll('strong')    
            current_price = strong[0].getText()[1:]
            sold_total = strong[1].getText()
            
            discount = item.findAll("strong",attrs={'class':'zhekou'})[0].contents[0][:-1]
        
            # create group_info
            group_info = GroupInfo()
            group_info.site = group_site
            group_info.title = title
            group_info.current_price = float(current_price)
            #group_info.original_price = float(original_price)
            group_info.discount = float(discount)
            group_info.picture = db.Link(picture)
            group_info.link = db.Link(link)
            group_info.sold_total = int(sold_total)
            group_info.create()
            
            group_infos.append(group_info)

    else:
        for group in today_groups:
            group_infos.append(group)
        
    return group_infos


